﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CreateFileAction.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the CreateFileAction type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ACME.Saf.Actions
{
    using System;
    using System.IO;

    using Collaboris.Saf.Actions.Wss;
    using Collaboris.Saf.Engine;
    using Collaboris.SharePoint.Wss.Entities;

    public class CreateFileAction : WSSActionBase<FileParameters>
    {
        /// <summary>
        /// Does the action.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        protected override ProcessStatus DoAction(SafContext context, FileParameters entity)
        {
            if (File.Exists(entity.FilePath))
                File.Delete(entity.FilePath);

            // Open the file and write the message inside it.
            StreamWriter sw = new StreamWriter(entity.FilePath, true);
            sw.WriteLine("I am an example of a file that has been created by a custom action outside of SAF.");
            sw.Flush();
            sw.Close();

            return ProcessStatus.Success;
        }

        /// <summary>
        /// Undoes the action.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        protected override ProcessStatus UndoAction(SafContext context, FileParameters entity)
        {
            if (File.Exists(entity.FilePath))
                File.Delete(entity.FilePath);

            return ProcessStatus.Success;
        }

        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">The <see cref="SafContext"/> passed in by the SAF Engine.</param>
        /// <param name="entity">The data entity to Validate.</param>
        /// <param name="errors">List of <see cref="ValidationError"/>s</param>
        /// <param name="mode"><see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.</param>
        /// <returns>false if invalid; otherwise true.</returns>
        protected override bool IsValid(SafContext context, FileParameters entity, ref System.Collections.Generic.List<ValidationError> errors, ProcessMode mode)
        {
            int currentCount = errors.Count;
            
            ValidateStringForNull(entity.FilePath, "ContentTypeToListMapping.ListName", ref errors);
     
            // Have we added errors ?
            return errors.Count == currentCount;
        }
    }

    /// <summary>
    /// Example Entity
    /// </summary>
    public class FileParameters : WSSEntityBase
    {
        private string _filePath;

        public string FilePath
        {
            get
            {
                return _filePath;
            }

            set
            {
                _filePath = value;
            }
        }
    }
}
